package validation.jexp;

import com.intellij.util.Alarm;
import com.jgoodies.binding.PresentationModel;
import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.beans.PropertyConnector;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.factories.ButtonBarFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.event.ActionEvent;

/**
 * Builds an editor with components bound to the domain object properties
 * using adapting ValueModels created by a PresentationModel.
 *
 * @author Karsten Lentzsch
 * @version $Revision: 1.7 $
 *
 * @see com.jgoodies.binding.PresentationModel
 */
public class EditorBoundExample {

    private final AlbumPresentationModel presentationModel;
    private JTextComponent titleField;
    private JTextComponent artistField;
    private JCheckBox classicalBox;
    private JTextComponent composerField;
    private JButton closeButton;

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.jgoodies.look.plastic.PlasticXPLookAndFeel");
        } catch (Exception ex) {
        }

        JFrame frame = new JFrame();
        frame.setTitle("BindingTutorial::Editor(Bound)");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        EditorBoundExample example = new EditorBoundExample();
        JComponent panel = example.build();
        frame.getContentPane().add(panel);
        frame.pack();
        JGoodiesValidationUtils.locateOnScreenCenter(frame);
        frame.setVisible(true);
    }

    public EditorBoundExample() {
        this(Album.ALBUM1);
    }

    public EditorBoundExample(Album album) {
        this.presentationModel = new AlbumPresentationModel(album);
    }

    private void initComponents() {
        titleField = BasicComponentFactory.createTextField(presentationModel.getModel(Album.PROPERTY_TITLE));
        artistField = BasicComponentFactory.createTextField(presentationModel.getModel(Album.PROPERTY_ARTIST));
        classicalBox = BasicComponentFactory.createCheckBox(presentationModel.getModel(Album.PROPERTY_CLASSICAL), "Classical");
        composerField = BasicComponentFactory.createTextField(presentationModel.getModel(Album.PROPERTY_COMPOSER));
        closeButton = new JButton(new CloseAction());
        boolean composerEnabled = presentationModel.isComposerEnabled();
        composerField.setEnabled(composerEnabled);
    }

    private void initEventHandling() {
        PropertyConnector.connect(composerField, "enabled", presentationModel,
                AlbumPresentationModel.PROPERTYNAME_COMPOSER_ENABLED);
        presentationModel.addBeanPropertyChangeListener(TutorialUtils.createDebugPropertyChangeListener());
    }

    public JComponent build() {
        initComponents();
        initEventHandling();
        FormLayout layout = new FormLayout(
                "right:pref, 3dlu, 150dlu:grow",
                "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, p");

        PanelBuilder builder = new PanelBuilder(layout);
        builder.setDefaultDialogBorder();
        CellConstraints cc = new CellConstraints();
        builder.addLabel("Title",      cc.xy (1, 1));
        builder.add(titleField,        cc.xy (3, 1));
        builder.addLabel("Artist",     cc.xy (1, 3));
        builder.add(artistField,       cc.xy (3, 3));
        builder.add(classicalBox,      cc.xy (3, 5));
        builder.addLabel("Composer",   cc.xy (1, 7));
        builder.add(composerField,     cc.xy (3, 7));
        builder.add(buildButtonBar(),  cc.xyw(1, 9, 3));

        return builder.getPanel();
    }

    private JComponent buildButtonBar() {
        return ButtonBarFactory.buildRightAlignedBar(closeButton);
    }

    private final class CloseAction extends AbstractAction {
        private CloseAction() {
            super("Close");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(presentationModel.getBean());
            System.exit(0);
        }
    }
}
